codewriting

You're trying to solve a puzzle that involves arranging squares of numbers according to their missing values. Each square has dimensions 4 × 4, containing all the numbers between 1 and 16 inclusively, except for one missing number represented by "?". All of these 4 × 4 squares are stored side-by-side within a larger matrix mat with dimensions 4 × (4 * n) (where n represents the number of square matrices).

Your task is to complete the following steps:

  • For each 4 × 4 square, find the value of the missing element and replace the "?" with this value.
  • Rearrange the squares inside the larger matrix by these missing values in ascending order. In the case of a tie (if two 4 × 4 matrices have the same missing value), place them in the relative order they were originally presented in the larger matrix mat.

Return the updated matrix mat as a result.

Example

  • For
mat = [["1",  "2",  "3",  "4"],
       ["?",  "5",  "6",  "10"],
       ["13", "16", "12", "15"],
       ["9",  "7",  "8",  "14"]]

the output should be

sortByMissingNum(mat) = [["1",  "2",  "3",  "4"],
                         ["11", "5",  "6",  "10"],
                         ["13", "16", "12", "15"],
                         ["9",  "7",  "8",  "14"]]

example 1

The given matrix mat contains only one 4 × 4 square, so we just find the missing value in it (11), and replace the "?" with this value.

  • For
mat = [["14", "3", "10", "4",  "16", "10", "?",  "2",  "?", "9",  "15", "11"],
       ["16", "7", "8",  "2",  "1",  "4",  "8",  "3",  "3", "16", "7",  "13"],
       ["?",  "9", "6",  "5",  "14", "12", "7",  "6",  "2", "10", "4",  "14"],
       ["15", "1", "13", "12", "9",  "15", "5",  "13", "1", "8",  "12", "6"]]

the output should be

sortByMissingNum(mat) = [["5", "9",  "15", "11", "14", "3",  "10", "4",  "16", "10", "11", "2"],
                         ["3", "16", "7",  "13", "16", "7",  "8",  "2",  "1",  "4",  "8",  "3"],
                         ["2", "10", "4",  "14", "11", "9",  "6",  "5",  "14", "12", "7",  "6"],
                         ["1", "8",  "12", "6",  "15", "1",  "13", "12", "9",  "15", "5", "13"]]

example 2

Input/Output

  • [execution time limit] 3 seconds (java)

  • [input] array.array.string mat

    A matrix of strings, where each element is equal to either "?" or a number from the inclusive range [1..16] in string form. It's guaranteed that the width of the given matrix is divisible by 4, and that each 4 × 4 square contains unique values.

    Guaranteed constraints:
    mat.length = 4,
    4 ≤ mat[i].length ≤ 500.

  • [output] array.array.string

    Return the resulting matrix, after mutating it as described in the problem description.

[Java] Syntax Tips

// Prints help message to the console
// Returns a string
// 
// Globals declared here will cause a compilation error,
// declare variables inside the function instead!
String helloWorld(String name) {
    System.out.println("This prints to the console when you Run Tests");
    return "Hello, " + name;
}

main.java
Java
Tests
Custom Tests
Run tests
Test 1
Test 2
Test 3
Test 4
Test 5
Test 6
Test 7
Test 8
Test 9
Test 10
Test 11
Test 12
Test 13
Test 14
Test 15
Test 16
Test 17
Test 18
Test 19

Formatting

Editor Mode
VS Code
Theme
Dark
Tab Size
Auto
Font Size
14px
Auto-brackets
Minimap
Code Completion
Error Highlighting

Hotkeys

CTRL/CMD + Enter
Submit
CTRL/CMD + R
Run
CTRL/CMD + S
Save
CTRL/CMD + Shift + >
Tab Right
CTRL/CMD + Shift + <
Tab Left